home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / oper_sys / fp / ifp_unix.lzh / ifp / interp / convert.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-05-23  |  4.0 KB  |  155 lines

  1.  
  2. /****** convert.c *****************************************************/
  3. /**                                                                  **/
  4. /**                    University of Illinois                        **/
  5. /**                                                                  **/
  6. /**                Department of Computer Science                    **/
  7. /**                                                                  **/
  8. /**   Tool: IFP                         Version: 0.5                 **/
  9. /**                                                                  **/
  10. /**   Author:  Arch D. Robison          Date:   May 1, 1985          **/
  11. /**                                                                  **/
  12. /**   Revised by: Arch D. Robison       Date:  July 2, 1986          **/
  13. /**                                                                  **/
  14. /**   Principal Investigators: Prof. R. H. Campbell                  **/
  15. /**                            Prof. W. J. Kubitz                    **/
  16. /**                                                                  **/
  17. /**                                                                  **/
  18. /**------------------------------------------------------------------**/
  19. /**   (C) Copyright 1987  University of Illinois Board of Trustees   **/
  20. /**                       All Rights Reserved.                       **/
  21. /**********************************************************************/
  22.  
  23. /* Type conversion functions */
  24.  
  25. #include <stdio.h>
  26. #include <ctype.h>
  27. #include "struct.h"
  28. #include "string.h"
  29. #include <math.h>
  30.  
  31. #define BUFSIZE 80 /* Maximum length of numeric string */
  32.  
  33. /*
  34.  * GetFPInt
  35.  *
  36.  * Get value of FP integer.
  37.  *
  38.  * Input
  39.  *    X = FP object
  40.  *
  41.  * Output
  42.  *    *K = FPint value of X
  43.  *    result = error code: 0 = X was converted to integer *K
  44.  *                         1 = X not an integer
  45.  *                         2 = X too big
  46.  */
  47. int GetFPInt (X,K)
  48.    ObjectPtr X;
  49.    FPint *K;
  50.    {
  51.       switch (X->Tag) {
  52.      default: return 1;
  53.      case INT: *K = X->Int; return 0;
  54.      case FLOAT: {
  55.         double F;
  56.         F = X->Float;
  57.         if (fabs (F) <= (double) FPMaxInt) {
  58.            *K = (FPint) F;
  59.            F -= (double) *K;
  60.            return fabs (F) >= CompTol;
  61.         } else return 2;
  62.      }
  63.       }
  64.    }
  65.  
  66. #if OPSYS==CTSS
  67. /*
  68.  * IsFloat
  69.  *
  70.  * Determine if a string represents floating point number as defined
  71.  * by C's atof function.  This function is necessary for the CRAY
  72.  * since there is a bug in sscanf for the CRAY.
  73.  *
  74.  * Input
  75.  *    S = string
  76.  *
  77.  * Output
  78.  *    result = true iff string represents number.
  79.  */
  80. int IsFloat (S)
  81.    register char *S;
  82.    {
  83.       int Digits = 0;
  84.       if (*S == '+' || *S == '-') S++;
  85.       while (isdigit (*S)) {
  86.      S++;
  87.      Digits++;
  88.       }
  89.       if (*S == '.') 
  90.      while (isdigit (*++S)) Digits++;
  91.       if (!Digits) return 0;
  92.       if (*S == '\0') return 1;
  93.       if (*S++ != 'e') return 0;
  94.       if (*S == '+' || *S == '-') S++;
  95.       while (isdigit (*S)) S++;
  96.       return *S == '\0';
  97.    }
  98. #endif /* OPSYS==CTSS */
  99.  
  100. /*
  101.  * StrToFloat
  102.  *
  103.  * Convert object to float representation if possible.
  104.  *
  105.  * Input
  106.  *    *X = object
  107.  *
  108.  * Output
  109.  *    *X = new representation of object
  110.  *    result = 1 if *X is float, 0 otherwise.
  111.  */
  112. boolean StrToFloat (X)
  113.    ObjectPtr X;
  114.    {
  115.       CharPtr U;
  116.       char Buf[BUFSIZE+1];
  117.       double F;
  118. #if OPSYS!=CTSS
  119.       char Term;
  120. #endif
  121.       CPInit (&U,&X->String);
  122.       (void) CPRead (&U,Buf,BUFSIZE);
  123.  
  124. #if OPSYS==CTSS
  125.       if (!IsFloat (Buf)) return 0;
  126.       F = atof (Buf);
  127. #else 
  128.       Buf [strlen (Buf)] = '\1';
  129.       if (2 != sscanf (Buf,"%lf%c",&F,&Term) || Term != '\1') return 0;
  130. #endif
  131.       RepTag (X,FLOAT);
  132.       X->Float = (FPfloat) F;
  133.       return 1;
  134.    }
  135.  
  136. /*
  137.  * GetDouble
  138.  *
  139.  * Output
  140.  *    result = 0 if *D is valid, 1 otherwise.
  141.  */
  142. int GetDouble (X,D)
  143.    ObjectPtr X;
  144.    double *D;
  145.    {
  146.      switch (X->Tag) {
  147.     case INT:   *D = X->Int; return 0;
  148.     case FLOAT: *D = X->Float; return 0;
  149.     default: return 1;
  150.       }
  151.    }
  152.  
  153.  
  154. /****************************** end of convert.c *****************************/
  155.